package com.ybkj.daijia.server.freight;

import com.fasterxml.jackson.annotation.JsonIgnore;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.util.StringUtils;

public class OrderOrbit implements Serializable {

    private static final long serialVersionUID = 1L;
    private Long id;
    private Long orderId;
    private Date created;
    private Date updated;
    @JsonIgnore
    private String xml; // xml 文件,包含坐标信息
    private List<OrderLngLat> lngAndLat = new LinkedList<OrderLngLat>();// 坐标集合

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getOrderId() {
        return orderId;
    }

    public void setOrderId(Long orderId) {
        this.orderId = orderId;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public Date getUpdated() {
        return updated;
    }

    public void setUpdated(Date updated) {
        this.updated = updated;
    }

    public String getXml() {
        return xml;
    }

    public void setXml(String xml) {
        this.xml = xml == null ? null : xml.trim();
    }

    public List<OrderLngLat> getLngAndLat() {
        if (!StringUtils.isEmpty(xml)) {
            try {

                lngAndLat = new ArrayList<OrderLngLat>();

                Document doc2 = DocumentHelper.parseText(xml);
                Element root2 = doc2.getRootElement();

                @SuppressWarnings("unchecked")
                Iterator<Element> iter = root2.elementIterator("location");
                while (iter.hasNext()) {
                    Element el = iter.next();
                    OrderLngLat orderLngLat = new OrderLngLat();
                    orderLngLat.setLng(Double.valueOf(el.attributeValue("longitude")));
                    orderLngLat.setLat(Double.valueOf(el.attributeValue("latitude")));
                    lngAndLat.add(orderLngLat);
                }
            } catch (DocumentException e) {
                e.printStackTrace();
            }
        }
        return lngAndLat;
    }

    public void setLngAndLat(List<OrderLngLat> lngAndLat) {
        this.lngAndLat = lngAndLat;
    }

    @Override
    public boolean equals(Object that) {
        if (this == that) {
            return true;
        }
        if (that == null) {
            return false;
        }
        if (getClass() != that.getClass()) {
            return false;
        }
        OrderOrbit other = (OrderOrbit) that;
        return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
            && (this.getOrderId() == null ? other.getOrderId() == null
            : this.getOrderId().equals(other.getOrderId()))
            && (this.getCreated() == null ? other.getCreated() == null
            : this.getCreated().equals(other.getCreated()))
            && (this.getUpdated() == null ? other.getUpdated() == null
            : this.getUpdated().equals(other.getUpdated()))
            && (this.getXml() == null ? other.getXml() == null
            : this.getXml().equals(other.getXml()));
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
        result = prime * result + ((getOrderId() == null) ? 0 : getOrderId().hashCode());
        result = prime * result + ((getCreated() == null) ? 0 : getCreated().hashCode());
        result = prime * result + ((getUpdated() == null) ? 0 : getUpdated().hashCode());
        result = prime * result + ((getXml() == null) ? 0 : getXml().hashCode());
        return result;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", id=").append(id);
        sb.append(", orderId=").append(orderId);
        sb.append(", created=").append(created);
        sb.append(", updated=").append(updated);
        sb.append(", xml=").append(xml);
        sb.append(", serialVersionUID=").append(serialVersionUID);
        sb.append("]");
        return sb.toString();
    }
}